package com.zd.wechatPlatform.service.impl.msgHandler;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zd.wechatPlatform.bean.model.WxAccount;
import com.zd.wechatPlatform.bean.model.WxFeebackMsg;
import com.zd.wechatPlatform.bean.model.WxKeywordMsg;
import com.zd.wechatPlatform.bean.vo.WxFeebackMsgVo;
import com.zd.wechatPlatform.cache.map.WxAccountCache;
import com.zd.wechatPlatform.common.enums.FeebackMsgTypeEnum;
import com.zd.wechatPlatform.common.utils.LogUtils;
import com.zd.wechatPlatform.common.utils.WeixinInterfaceUtil;
import com.zd.wechatPlatform.dao.WxFeebackMsgDao;
import com.zd.wechatPlatform.service.IWeixinMsgHandler;
import com.zd.wechatPlatform.service.IWxKeywordMsgService;

/**
 * 微信文本消息处理器
 * 
 * @author lishi
 * @date 2018年11月1日 上午9:40:54
 */
@Service("textMsgHandler")
public class WeixinTextMsgHandler implements IWeixinMsgHandler {

	@Autowired
	private IWxKeywordMsgService wxKeywordMsgService;

	@Autowired
	private WxAccountCache wxAccountCache;

	@Autowired
	private WxFeebackMsgDao WxFeebackMsgDao;

	public void handlerMsg(Map<String, String> msgMap) throws Exception {
		String accountId = msgMap.get("ToUserName");
		String openId = msgMap.get("FromUserName");
		String content = msgMap.get("Content");

		WxAccount wxAccount = wxAccountCache.get(accountId);
		if (wxAccount != null) {
			List<WxKeywordMsg> keywordMsgList = wxKeywordMsgService.findKeywordMsg(wxAccount.getId());

			String accessToken = wxAccount.getAccessToken();

			//查找匹配的关键字，暂时只支持一个关键字回复一条消息
			WxKeywordMsg matchKeyword = null;
			if (keywordMsgList != null && keywordMsgList.size() > 0) {
				for (WxKeywordMsg keywordMsg : keywordMsgList) {
					String keywords = keywordMsg.getKeyword();
					String[] keywordArray = keywords.split("\\|");
					for (String keyword : keywordArray) {
						if (content.indexOf(keyword) != -1) {
							matchKeyword = keywordMsg;
							break;
						}
					}

					if (matchKeyword != null) {
						break;
					}
				}
			}
			if (matchKeyword == null) {
				//当用户输入的信息匹配不到任何关键字时，获取数据库最后一个关键字回复配置
				matchKeyword = keywordMsgList.get(keywordMsgList.size() - 1);
			}

			WxFeebackMsg feebackMsg = WxFeebackMsgDao.get(matchKeyword.getFeebackMsgId());
			if (FeebackMsgTypeEnum.text.getKey() == feebackMsg.getMsgType()) {
				//给用户推送文本消息
				WxFeebackMsgVo feebackMsgVo = new WxFeebackMsgVo(openId, FeebackMsgTypeEnum.text.getValue());
				feebackMsgVo.setContent(feebackMsg.getContent());
				String respone = WeixinInterfaceUtil.sendMsg(accessToken, feebackMsgVo.getJSONMsg());
				LogUtils.info("respone : " + respone);
			} else if (FeebackMsgTypeEnum.image.getKey() == feebackMsg.getMsgType()) {
				//TODO
			} else if (FeebackMsgTypeEnum.voice.getKey() == feebackMsg.getMsgType()) {
				//TODO
			} else if (FeebackMsgTypeEnum.video.getKey() == feebackMsg.getMsgType()) {
				//TODO
			} else if (FeebackMsgTypeEnum.music.getKey() == feebackMsg.getMsgType()) {
				//TODO
			} else if (FeebackMsgTypeEnum.news.getKey() == feebackMsg.getMsgType()) {
				//TODO
			}
		} else {
			LogUtils.error(accountId + " 没有配置进数据库");
		}
	}
}
